#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1010;
int n, m, s, t;
int g[N][N];
int dis[N];
int a, b, c;
bool st[N];
void dij()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	for (int i = 0; i < n; i++) {
		int t = -1;
		for (int j = 1; j <= n; j++) {
			if (!st[j] && (t == -1 || dis[j] < dis[t])) {
				t = j;
			}
		}
		st[t] = 1;
		for (int j = 1; j <= n; j++) {
			dis[j] = min(dis[j], dis[t] + g[t][j]);
		}
	}
}
int main()
{
	cin >> n >> m >> s >> t;
	memset(g, 0x3f, sizeof(g));
	while (m--) {
		cin >> a >> b >> c;
		g[a][b] = g[b][a] = min(g[a][b], c);
	}
	dij();
	if (dis[t] == 0x3f3f3f3f) {
		cout << -1 << endl;
	}
	else {
		cout << dis[t] << endl;
	}
	return 0;
}